package v1

import (
	"context"
	"time"

	"wxpay/internal/dto"
	"wxpay/internal/service"
	wxpay "wxpay/pkg/api/v1"
	"wxpay/pkg/merrors"
)

func (s *Server) convertFaceToFaceSignDTO(req *wxpay.AliFaceToFaceSignRequest) *dto.AlipayAgentSignDTO {
	if req == nil {
		return nil
	}

	contactDTO := &dto.AliSignContactInfoDTO{
		ContactName:   req.ContactName,
		ContactMobile: req.ContactMobile,
		ContactEmail:  req.ContactEmail,
	}
	signInfoDTO := &dto.AliFaceToFaceSignInfoDTO{
		MccCode:                req.MccCode,
		SpecialLicensePic:      req.SpecialLicensePic,
		Rate:                   req.Rate,
		SignAndAuth:            req.SignAndAuth,
		BusinessLicenseNo:      req.BusinessLicenseNo,
		BusinessLicensePic:     req.BusinessLicensePic,
		BusinessLicenseAuthPic: req.BusinessLicenseAuthPic,
		LongTerm:               req.LongTerm,
		DateLimitation:         req.DateLimitation,
		ShopScenePic:           req.ShopScenePic,
		ShopSignBoardPic:       req.ShopSignBoardPic,
	}
	signDTO := &dto.AlipayAgentSignDTO{
		BrandID:       req.BrandId,
		AppID:         req.AppId,
		AlipayAccount: req.AlipayAccount,
		ContactInfo:   contactDTO,
		SignInfo:      signInfoDTO,
		BizApplyNo:    req.BizNo,
		EmpID:         req.EmpId,
		CreateTime:    time.Now(),
		UpdateTime:    time.Now(),
	}
	return signDTO
}

// 提交当面付签约申请
func (s *Server) AliPayFaceToFaceSignSubmit(ctx context.Context, req *wxpay.AliFaceToFaceSignRequest) (*wxpay.AliFaceToFaceSignResponse, error) {
	op := merrors.Op("AliPayFaceToFaceSignSubmit")
	applyDto := s.convertFaceToFaceSignDTO(req)
	res, err := service.NewAlipayAgentService(s.d).FaceToFaceSignSubmit(ctx, applyDto)
	if err != nil {
		return nil, merrors.E(op, merrors.Invalid, err)
	}
	return &wxpay.AliFaceToFaceSignResponse{
		BrandId:        res.BrandID,
		ApplyNo:        res.ApplyNo,
		AlipayAccount:  res.AlipayAccount,
		ApplyState:     res.ApplyState,
		ApplyStateDesc: res.ApplyStateDesc,
	}, nil
}

// 提交当面付签约申请
func (s *Server) AliPayFaceToFaceSignReSubmit(ctx context.Context, req *wxpay.AliFaceToFaceSignRequest) (*wxpay.AliFaceToFaceSignResponse, error) {
	op := merrors.Op("AliPayFaceToFaceSignReSubmit")
	applyDto := s.convertFaceToFaceSignDTO(req)
	res, err := service.NewAlipayAgentService(s.d).FaceToFaceSignReSubmit(ctx, applyDto)
	if err != nil {
		return nil, merrors.E(op, merrors.Invalid, err)
	}
	return &wxpay.AliFaceToFaceSignResponse{
		BrandId:        res.BrandID,
		ApplyNo:        res.ApplyNo,
		AlipayAccount:  res.AlipayAccount,
		ApplyState:     res.ApplyState,
		ApplyStateDesc: res.ApplyStateDesc,
	}, nil
}

func adaptFaceToFaceSignQuery(d *dto.AlipayAgentSignDTO) *wxpay.AliFaceToFaceSignQueryResponse {
	if d == nil {
		return nil
	}
	r := &wxpay.AliFaceToFaceSignQueryResponse{
		ApplyNo:        d.ApplyNo,
		BrandId:        d.BrandID,
		AlipayAccount:  d.AlipayAccount,
		ApplyState:     d.ApplyState,
		ApplyStateDesc: d.ApplyStateDesc,
		ConfirmUrl:     d.ConfirmUrl,
	}
	if d.ContactInfo != nil {
		r.ContactName = d.ContactInfo.ContactName
		r.ContactMobile = d.ContactInfo.ContactMobile
		r.ContactEmail = d.ContactInfo.ContactEmail
	}
	if d.SignInfo != nil {
		r.MccCode = d.SignInfo.MccCode
		r.SpecialLicensePic = d.SignInfo.SpecialLicensePic
		r.SpecialLicensePicUrl = buildOssUrl(d.SignInfo.SpecialLicensePic)
		r.Rate = d.SignInfo.Rate
		r.SignAndAuth = d.SignInfo.SignAndAuth
		r.BusinessLicenseNo = d.SignInfo.BusinessLicenseNo
		r.BusinessLicensePic = d.SignInfo.BusinessLicensePic
		r.BusinessLicensePicUrl = buildOssUrl(d.SignInfo.BusinessLicensePic)
		r.BusinessLicenseAuthPic = d.SignInfo.BusinessLicenseAuthPic
		r.BusinessLicenseAuthPicUrl = buildOssUrl(d.SignInfo.BusinessLicenseAuthPic)
		r.LongTerm = d.SignInfo.LongTerm
		r.DateLimitation = d.SignInfo.DateLimitation
		r.ShopScenePic = d.SignInfo.ShopScenePic
		r.ShopScenePicUrl = buildOssUrl(d.SignInfo.ShopScenePic)
		r.ShopSignBoardPic = d.SignInfo.ShopSignBoardPic
		r.ShopSignBoardPicUrl = buildOssUrl(d.SignInfo.ShopSignBoardPic)
	}
	if d.AuditDetail != nil {
		r.RejectReason = d.AuditDetail.RejectReason
	}
	return r
}

func (s *Server) AliPayFaceToFaceSignQuery(ctx context.Context, req *wxpay.AliFaceToFaceSignQueryRequest) (*wxpay.AliFaceToFaceSignQueryResponse, error) {
	op := merrors.Op("AliPayFaceToFaceSignQuery")
	if req == nil {
		return nil, nil
	}
	res, err := service.NewAlipayAgentService(s.d).FaceToFaceSignQuery(ctx, req.BrandId, req.AppId, req.BizNo, req.AlipayAccount, req.AuthToken)
	if err != nil {
		return nil, merrors.E(op, merrors.Invalid, err)
	}
	return adaptFaceToFaceSignQuery(res), nil
}

func (s *Server) AliPaySignStatusQuery(ctx context.Context, req *wxpay.AliSignStatusQueryRequest) (*wxpay.AliSignStatusQueryResponse, error) {
	op := merrors.Op("AliPaySignStatusQuery")
	if req == nil {
		return nil, nil
	}
	res, err := service.NewAlipayAgentService(s.d).AlipaySignStatusQuery(ctx, req.AppId, req.AuthToken, req.Pid, req.ProductCodes)
	if err != nil {
		return nil, merrors.E(op, merrors.Invalid, err)
	}
	signStatus := make([]*wxpay.AliSignStatus, len(res))
	for idx, r := range res {
		signStatus[idx] = &wxpay.AliSignStatus{
			ProductName: r.ProductName,
			ProductCode: r.ProductCode,
			Status:      r.Status,
			StatusDesc:  r.StatusDesc,
		}
	}
	return &wxpay.AliSignStatusQueryResponse{
		SignStatusList: signStatus,
	}, nil
}
